package cn.edu.neusoft.wuguangsheng.flowerimpression.fragment;


import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.DialogThridUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.SearchShopGoodsActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.WeiboDialogUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.ShopAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.LunBoTuBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.ShopInfoBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetLunBoTuModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetShopListModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShopShouYFragment extends BaseFragment implements View.OnClickListener {
    ImageView shop_shoyye_fh;

    private List<ShopInfoBean> items;
    RecyclerView rv_shoplist;

    private TextView tv_search_home;


    private Banner mBanner;
    private ArrayList<String> images;
    private ArrayList<String> imageTitle;
    private Dialog mDialog;
    @Override
    void initView() {
        shop_shoyye_fh = (ImageView)view.findViewById(R.id.shop_shoyye_fh);

        rv_shoplist = (RecyclerView)view.findViewById(R.id.rv_shoplist);
        tv_search_home = (TextView)view.findViewById(R.id.tv_search_home);

        mBanner = (Banner)view.findViewById(R.id.banner);
        mDialog = WeiboDialogUtils.createLoadingDialog(getContext(), "获取中...");
        GetShopListModel getShopListModel = new GetShopListModel();
        getShopListModel.getShopList(shopInfoBeanListListener);

        GetLunBoTuModel getLunBoTuModel = new GetLunBoTuModel();
        getLunBoTuModel.getLunBoTu(new ListListener<LunBoTuBean>() {
            @Override
            public void onResponse(List<LunBoTuBean> list) {
                images = new ArrayList<>();
                imageTitle = new ArrayList<>();
for (int i=0;i<list.size();i++){

    images.add(Service.BASE_URL+"flower_impression/public/uploads/"+list.get(i).getLbt_name());
    imageTitle.add(list.get(i).getLbt_content());
}

                //设置样式,默认为:Banner.NOT_INDICATOR(不显示指示器和标题)
                //可选样式如下:
                //1. Banner.CIRCLE_INDICATOR    显示圆形指示器
                //2. Banner.NUM_INDICATOR   显示数字指示器
                //3. Banner.NUM_INDICATOR_TITLE 显示数字指示器和标题
                //4. Banner.CIRCLE_INDICATOR_TITLE  显示圆形指示器和标题
                //设置banner样式
                mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
                //设置图片加载器
                mBanner.setImageLoader(new GlideImageLoader());
                //设置标题集合（当banner样式有显示title时）
                mBanner.setBannerTitles(imageTitle);
                //设置轮播样式（没有标题默认为右边,有标题时默认左边）
                //可选样式:
                //Banner.LEFT   指示器居左
                //Banner.CENTER 指示器居中
                //Banner.RIGHT  指示器居右
                mBanner.setIndicatorGravity(BannerConfig.CENTER);
                //设置是否允许手动滑动轮播图
                mBanner.setViewPagerIsScroll(true);
                //设置是否自动轮播（不设置则默认自动）
                mBanner.isAutoPlay(true);
                //设置轮播图片间隔时间（不设置默认为2000）
                mBanner.setDelayTime(1500);
                //设置图片资源:可选图片网址/资源文件，默认用Glide加载,也可自定义图片的加载框架
                //所有设置参数方法都放在此方法之前执行
                mBanner.setIndicatorGravity(BannerConfig.CENTER);
                mBanner.setImages(images)
                        .setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(int position) {
                               // Toast.makeText(getContext(), "你点了第" + (position + 1) + "张轮播图", Toast.LENGTH_SHORT).show();
                               Toast.makeText(getContext(), imageTitle.get(position), Toast.LENGTH_SHORT).show();
                            }
                        })
                        .start();
                DialogThridUtils.closeDialog(mDialog);
            }

            @Override
            public void onFail(String msg) {
                DialogThridUtils.closeDialog(mDialog);

            }
        });
    }

    @Override
    void initEvent() {
        shop_shoyye_fh.setOnClickListener(this);
        tv_search_home.setOnClickListener(this);
    }

    @Override
    void initDate() {

        //设置图片资源:url或本地资源

    }

    ListListener<ShopInfoBean> shopInfoBeanListListener = new ListListener<ShopInfoBean>() {
        @Override
        public void onResponse(List<ShopInfoBean> list) {
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rv_shoplist.setLayoutManager(linearLayoutManager);
            items=list;
            ShopAdapter shopAdapter=new ShopAdapter(getContext(),items,R.layout.shop_item);
            rv_shoplist.setAdapter(shopAdapter);
            rv_shoplist.setItemAnimator(new DefaultItemAnimator());
           // rv_shoplist.setEmptyView(mEmptyView);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    public ShopShouYFragment() {
        // Required empty public constructor
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.shop_shoyye_fh:
                getActivity().finish();
                break;
            case R.id.tv_search_home:
                Intent intent = new Intent(getContext(), SearchShopGoodsActivity.class);
                startActivity(intent);
                break;
        }
    }







    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setLayout_file(R.layout.fragment_shop_shou_y);
        return super.onCreateView(inflater,container,savedInstanceState);
    }


    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context)
                    .load((String) path)
                    .into(imageView);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onStop() {
        super.onStop();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

}


